package viewModel;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.table.AbstractTableModel;

import view.tables.IconizedInterface;
import view.tables.IconizedString;

import domain.Book;
import domain.Copy;
import domain.Customer;
import domain.Library;

@SuppressWarnings("serial")
public class CustomerSelectionTable extends AbstractTableModel implements Observer {
	Library lib = Library.getInstance();
	List<Customer> customers;
//	DateFormat localizeDate = DateFormat.getDateInstance();
	private String[] columnNames = new String[]{ "Kunde" };
	private int[] searchColumns = {0};
	
	public CustomerSelectionTable(){
		customers = lib.getCustomers();
		for (Customer c : customers) {
			c.addObserver(this);
		}
		lib.addObserver(this);
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return customers.size();
	}
	
	@Override
	public String getColumnName(int col){
		return columnNames[col];
	}
	
	public int[] getSearchColumns(){
		return searchColumns;
	}

	@Override
	public Object getValueAt(int row, int col) {
		Customer customer = customers.get(row);
		CustomerStatus cs = CustomerStatus.getInstanceOf(customer);
		switch (col) {
			case 0 :
				return new IconizedString(customer.toString(), cs.getIcon());
		}
		return null;
	}

	@Override
	public void update(Observable o, Object arg1) {
		if(arg1 instanceof Customer){
			Customer buffer = (Customer)arg1;
			buffer.addObserver(this);
		}
		fireTableDataChanged();
	}
	
	public Customer getCustomerAt(int row) {
		return customers.get(row);
	}
	
	//Ohne das weiss die Tabelle nicht welche klasse dahinter steckt (bei boolean wird automatisch checkbox erzeugt!)
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Class getColumnClass(int col) {
		switch (col) {
			case 0 :
				return IconizedString.class;
			default:
				return String.class;
		}
	}
	
	public void disbandObservers(){
		lib.deleteObserver(this);
		for(Customer c : lib.getCustomers()){
			c.deleteObserver(this);
		}
	}

}
